package edu.study.user.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import edu.study.api.common.dto.BaseRespDto;
import edu.study.api.common.dto.RespDto;
import edu.study.common.util.cache.Lock;
import edu.study.common.util.cache.LockExecuteCallback;
import edu.study.user.api.RoleUserApi;
import edu.study.user.api.dto.RoleUserDto;
import edu.study.user.api.req.roleuser.RoleUserReq;
import edu.study.user.api.req.roleuser.QueryRoleUserReq;
import edu.study.user.service.RoleUserService;

@RestController
public class RoleUserApiImpl implements RoleUserApi {

	@Autowired
    private Lock lock;

    @Autowired
    private RoleUserService roleUserService;

	@Override
	public BaseRespDto addRoleUser(@RequestBody RoleUserReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getRoleNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = roleUserService.addRoleUser(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto deleteRoleUser(@RequestBody RoleUserReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getRoleNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = roleUserService.deleteRoleUser(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto modifyRoleUser(@RequestBody RoleUserReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getRoleNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = roleUserService.modifyRoleUser(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public RespDto<List<RoleUserDto>> queryRoleUserList(@RequestBody QueryRoleUserReq req) {
		RespDto<List<RoleUserDto>> resp = roleUserService.queryRoleUserList(req);
		return resp;
	}
}
